package com.example.adminserivce.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.adminserivce.Service.StudentService;
import com.example.common.domain.Result;
import com.example.common.domain.dto.search.StudentSearchDto;
import com.example.common.domain.pojo.Student;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RefreshScope // 实时刷新配置信息
@Slf4j  // 日志 log.info("登录日志测试：");
@RequestMapping("/admins")
@Api(value = "管理员端接口", description = "学生信息管理")
@RequiredArgsConstructor

public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 根据id查询学生信息
     *
     * @param id 学生id
     * @return 学生信息
     */
    @GetMapping("/getStudentById/{id}")
    public Result<Student> getStudentById(@PathVariable String id) {
        return Result.success(studentService.getStudentByIdWithLogicalExpire(id));
    }

    /**
     * 分页条件查询学生信息
     *
     * @param searchDto 查询条件
     * @return 学生信息（分页）
     */
    @GetMapping("/getStudent")
    public Result<Page<Student>> getStudents(
            @RequestParam(defaultValue = "0") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            StudentSearchDto searchDto) {

        Page<Student> page = new Page<>(pageNum, pageSize);
        return Result.success(studentService.pageByCriteria(page, searchDto));
    }

    /**
     * 添加学生信息
     *
     * @param student 要添加的学生信息
     * @return 添加结果
     */
    @PostMapping("/addStudent")
    public Result<Boolean> addStudent(@RequestBody Student student) {
        return Result.success(studentService.save(student));
    }

    /**
     * 删除学生信息
     *
     * @param id 要删除的学生id
     *           状态删除
     */
    @DeleteMapping("/deleteStudent")
    public Result<Boolean> deleteStudent(@RequestParam String id) {
        return Result.success(studentService.removeById(id));
    }

    /**
     * 修改学生信息
     *
     * @param student 要修改的学生信息
     * @return 修改结果
     */
    @PostMapping("/updateStudent")
    public Result<Boolean> updateStudent(@RequestBody Student student) {
        return Result.success(studentService.updateById(student));
    }

    /**
     * 批量添加学生信息
     *
     * @param students 要添加的学生信息
     * @return 添加结果
     */
    @PostMapping("/addStudents")
    public Result<Boolean> addStudents(@RequestBody Student[] students) {
        return Result.success(studentService.saveBatch(Arrays.asList(students)));
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 要删除的学生id
     * @return 删除结果
     */
    @DeleteMapping("/deleteStudents")
    public Result<Boolean> deleteStudents(@RequestParam String[] ids) {
        return Result.success(studentService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 批量修改学生信息
     *
     * @param students 要修改的学生信息
     * @return 修改结果
     */
    @PostMapping("/updateStudents")
    public Result<Boolean> updateStudents(@RequestBody Student[] students) {
        return Result.success(studentService.updateBatchById(Arrays.asList(students)));
    }
}